﻿package mortal.game.view.guildNew.info
{
    import com.gengine.global.*;
    import com.gengine.keyBoard.*;
    import com.gengine.resource.*;
    import com.gengine.utils.*;
    import com.mui.controls.*;
    import extend.language.*;
    import flash.display.*;
    import flash.events.*;
    import mortal.common.global.*;
    import mortal.component.gconst.*;
    import mortal.game.cache.*;
    import mortal.game.cache.guild.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.display.*;
    import mortal.game.view.copy.guildCopy.*;
    import mortal.game.view.guildNew.WareHourse.*;
    import mortal.game.view.guildNew.info.data.*;
    import mortal.mvc.core.*;

    public class GuildNewInfoPanel extends GSprite implements GuildNewPanelI
    {
        protected var _bg:GImageBitmap;
        protected var _guildName:GTextFiled;
        protected var _levelTxt:BitmapNumberText;
        protected var _resourceTxt:GTextFiled;
        protected var _editBtn:GButton;
        protected var _txtGuildPurpose:GTextInput;
        protected var _list1:CellsGridSprite;
        protected var _list2:CellsGridSprite;
        protected var _guildBossNum:GuildNewWareHAppNum;
        protected var _worldBoss:GuildNewWareHAppNum;
        protected var _enterBtn:GLoadingButton;
        protected var _changeBtn:GButton;
        private var _oldPur:String;
        private var _isEditorPur:Boolean = false;

        public function GuildNewInfoPanel()
        {
            this._oldPur = "" + Language.getString(71471);
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            LoaderHelp.addResCallBack(ResFileConst.guildNewInfo, this.onResLoad);
            return;
        }// end function

        private function onResLoad() : void
        {
            this._bg = UIFactory.gImageBitmap(ImagesConst.GuildNewInfoBg, 0, 0, this);
            this._guildName = UIFactory.gTextField("", 384, 11, 200, 25, this, GlobalStyle.textFormatHuang2.setSize(17).setBold(true));
            this._levelTxt = UIFactory.bitmapNumberText(297, 10, "GuildWarEnterNum.png", 28, 29, -16, this, 13, 1);
            this._levelTxt = UIFactory.bitmapNumberText(297, 10, "GuildWarEnterNum.png", 28, 29, -16, this, 13, 1);
            this._levelTxt.text = "3";
            this._resourceTxt = UIFactory.gTextField("", 286, 47, 170, 25, this, GlobalStyle.textFormatHuang.center());
            this._editBtn = UIFactory.gButton("" + Language.getString(71472), 82, 286, 63, 30, this, "GreenLinkButton");
            this._txtGuildPurpose = this.createTextInput(158, 268, 509, 42, this);
            this._txtGuildPurpose.maxChars = 50;
            this._txtGuildPurpose.text = "";
            var _loc_1:Boolean = false;
            this._txtGuildPurpose.mouseEnabled = false;
            this._txtGuildPurpose.editable = _loc_1;
            this._enterBtn = UIFactory.gLoadingButton(ImagesConst.GuildNewEnterManorBtn_upSkin, 323, 72, 108, 108, this);
            this._enterBtn.label = "";
            this._enterBtn.configEventListener(MouseEvent.CLICK, this.onEnterManorHd);
            this._list1 = UIFactory.getUICompoment(CellsGridSprite);
            this._list1.colNum = 4;
            this._list1.vCellWid = 190;
            this._list1.hCellWid = 100;
            this._list1.genCellFun = UIFactory.getUICompoment;
            this._list1.genCells(4, GuildNewInfoBtnCell);
            this._list1.x = 46;
            this._list1.y = 195;
            this.addChild(this._list1);
            this._list2 = UIFactory.getUICompoment(CellsGridSprite);
            this._list2.colNum = 5;
            this._list2.vCellWid = 132;
            this._list2.hCellWid = 100;
            this._list2.genCellFun = UIFactory.getUICompoment;
            this._list2.genCells(5, GuildNewInfoBtnCell2);
            this._list2.x = 53;
            this._list2.y = 325;
            this.addChild(this._list2);
            this._guildBossNum = UIFactory.getUICompoment(GuildNewWareHAppNum);
            UIFactory.setObjAttri(this._guildBossNum, 140, 331, 20, 20);
            this._worldBoss = UIFactory.getUICompoment(GuildNewWareHAppNum);
            UIFactory.setObjAttri(this._worldBoss, 274, 332, 20, 20);
            this._changeBtn = UIFactory.gButton("" + Language.getString(71473), 478, 11, 50, 27, this);
            this._changeBtn.x = this._guildName.x + this._guildName.text.length * 17 + 20;
            this._changeBtn.configEventListener(MouseEvent.CLICK, this.onChangeNameHd);
            this._changeBtn.visible = false;
            this._resourceTxt.toolTipData = "" + Language.getString(71474);
            this._editBtn.configEventListener(MouseEvent.CLICK, this.onEditorHd);
            Dispatcher.dispatchEvent(new DataEvent(EventName.GUILD_INFO_GET));
            if (!Cache.instance.guild.selfGuildInfo.wareHouse.hasItemData)
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.Guild_wareHouse_getData));
            }
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            this._isEditorPur = false;
            LoaderHelp.removeResEvent(ResFileConst.guildNewInfo, this.onResLoad);
            this._bg = null;
            this._guildName = null;
            this._levelTxt = null;
            this._resourceTxt = null;
            this._editBtn = null;
            this._txtGuildPurpose = null;
            this._list1 = null;
            this._list2 = null;
            this._changeBtn = null;
            this._enterBtn = null;
            this._guildBossNum = null;
            this._worldBoss = null;
            if (GuildNewInfoChangeName.isInShow)
            {
                GuildNewInfoChangeName.instance.hide();
            }
            super.disposeImpl(param1);
            return;
        }// end function

        private function initInfo() : void
        {
            if (Cache.instance.guild.selfGuildInfo.baseInfo == null)
            {
                return;
            }
            var _loc_1:* = GuildNInfoUtil.getMidDataArr();
            if (this._list1 == null)
            {
                return;
            }
            this._list1.updateAllNotDispose(_loc_1, GuildNewInfoBtnCell, "updateInfo");
            var _loc_2:* = GuildNInfoUtil.getBottomArr();
            this._list2.updateAllNotDispose(_loc_2, GuildNewInfoBtnCell2, "updateInfo");
            this.updateGuildName();
            this.updateResource();
            this.updatePurpose();
            this.updateChangeBtn();
            if (GuildConst.hasPermissions(GuildConst.ChangePurpose))
            {
                this._txtGuildPurpose.configEventListener(FocusEvent.FOCUS_IN, this.onTxtFocusIn);
            }
            this.updateGuildBoss();
            this.updateWorldBoss();
            return;
        }// end function

        private function onEnterManorHd(event:MouseEvent) : void
        {
            var _loc_2:* = Cache.instance.guild.selfGuildInfo;
            Dispatcher.dispatchEvent(new DataEvent(EventName.Guild_enterManor, _loc_2.baseInfo.guildId));
            return;
        }// end function

        public function updateGuildBoss() : void
        {
            var _loc_1:* = GuildNInfoUtil.getGuildCopyNum();
            this._guildBossNum.updateNum(_loc_1);
            return;
        }// end function

        public function updateWorldBoss() : void
        {
            var _loc_1:* = GuildNInfoUtil.getWorldBossNum();
            var _loc_2:* = Cache.instance.copy.guildCopy;
            if (!_loc_2.isOpen)
            {
                _loc_1 = 0;
            }
            this._worldBoss.updateNum(_loc_1);
            return;
        }// end function

        public function update() : void
        {
            this.initInfo();
            return;
        }// end function

        private function updateGuildName() : void
        {
            if (Cache.instance.guild.selfGuildInfo)
            {
                this._guildName.text = "" + Cache.instance.guild.selfGuildInfo.baseInfo.guildName;
                this._levelTxt.text = "" + Cache.instance.guild.selfGuildInfo.baseInfo.level;
                this._changeBtn.x = this._guildName.x + this._guildName.text.length * 17 + 20;
            }
            return;
        }// end function

        private function updateResource() : void
        {
            var _loc_1:* = Cache.instance.guild.selfGuildInfo;
            var _loc_2:* = _loc_1.baseInfo.resource >= _loc_1.getLevelConfig().upgradeResource;
            if (_loc_2)
            {
                this._resourceTxt.htmlText = Language.getStringByParam(71475, _loc_1.baseInfo.resource, _loc_1.getLevelConfig().upgradeResource);
            }
            else
            {
                this._resourceTxt.htmlText = Language.getStringByParam(71476, HTMLUtil.addColor(_loc_1.baseInfo.resource.toString(), GlobalStyle.colorHong), _loc_1.getLevelConfig().upgradeResource);
            }
            return;
        }// end function

        private function onEditorHd(event:MouseEvent) : void
        {
            if (this._txtGuildPurpose)
            {
                if (Cache.instance.guild.selfGuildInfo.baseInfo.level < 3)
                {
                    MsgManager.showRollTipsMsg("" + Language.getString(71477));
                    return;
                }
                if (Cache.instance.role.entityInfo.combat < 30000)
                {
                    MsgManager.showRollTipsMsg("" + Language.getString(71478));
                    return;
                }
                this._txtGuildPurpose.editable = true;
                if (this._oldPur == this._txtGuildPurpose.text)
                {
                    this._txtGuildPurpose.text = "";
                }
                this.setChatInputFocus(true);
            }
            return;
        }// end function

        public function setChatInputFocus(param1:Boolean) : void
        {
            if (param1)
            {
                this._txtGuildPurpose.setFocus();
                Global.instance.callLater(this.selectLastChat);
            }
            else
            {
                Global.stage.focus = Global.stage;
                KeyBoardManager.instance.changeImeEnable(false);
            }
            return;
        }// end function

        public function selectLastChat() : void
        {
            this._txtGuildPurpose.setSelection(0, 0);
            return;
        }// end function

        private function updatePurpose() : void
        {
            if (this._isEditorPur)
            {
                return;
            }
            this._txtGuildPurpose.editable = false;
            this._editBtn.visible = GuildConst.hasPermissions(GuildConst.ChangePurpose);
            var _loc_1:* = "" + Cache.instance.guild.selfGuildInfo.baseInfo.purpose;
            if (_loc_1 == "")
            {
                _loc_1 = this._oldPur;
            }
            this._txtGuildPurpose.text = _loc_1;
            if (this._oldPur == _loc_1)
            {
                this._txtGuildPurpose.filters = [FilterConst.colorFilter2];
            }
            else
            {
                this._txtGuildPurpose.filters = null;
            }
            return;
        }// end function

        public function createTextInput(param1:int, param2:int, param3:int, param4:int, param5:DisplayObjectContainer) : GTextInput
        {
            var _loc_6:* = UIFactory.gTextInput();
            UIFactory.setObjAttri(_loc_6, param1, param2, param3, param4, this);
            var _loc_7:* = new Bitmap();
            _loc_6.setStyle("upSkin", _loc_7);
            _loc_6.setStyle("disabledSkin", _loc_7);
            _loc_6.setStyle("textFormat", GlobalStyle.textFormatPutong);
            _loc_6.setStyle("focusRectSkin", "");
            _loc_6.textField.filters = [FilterConst.glowFilter];
            return _loc_6;
        }// end function

        private function onTxtFocusIn(event:FocusEvent) : void
        {
            if (this._txtGuildPurpose.editable == false)
            {
                return;
            }
            this._isEditorPur = true;
            this._txtGuildPurpose.configEventListener(FocusEvent.FOCUS_OUT, this.onTxtFocusOut);
            return;
        }// end function

        private function onTxtFocusOut(event:FocusEvent) : void
        {
            this._isEditorPur = false;
            this._txtGuildPurpose.removeEventListener(FocusEvent.FOCUS_OUT, this.onTxtFocusOut);
            this._txtGuildPurpose.editable = false;
            this.setChatInputFocus(false);
            Dispatcher.dispatchEvent(new DataEvent(EventName.GUILD_PURPOSE_CHANGE, this._txtGuildPurpose.text));
            return;
        }// end function

        public function updateMemberNum() : void
        {
            var _loc_1:GuildNewInfoBtnCell = null;
            var _loc_2:Object = null;
            if (this._list1)
            {
                _loc_1 = this._list1.cellArr[1];
                _loc_2 = GuildNInfoUtil.getInfoMemberNum();
                if (_loc_2)
                {
                    _loc_1.updateInfo(_loc_2);
                }
            }
            return;
        }// end function

        private function onChangeNameHd(event:MouseEvent) : void
        {
            GuildNewInfoChangeName.instance.show();
            return;
        }// end function

        public function updateChangeBtn() : void
        {
            if (this._changeBtn)
            {
                this._changeBtn.visible = GuildNInfoUtil.canShowChangeBtn();
            }
            return;
        }// end function

    }
}
